<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
use org\Qiniu;
class Timing extends Base {
    /**
     * 会议状态、拜访状态（1分一跑）
     * 会议开始提醒和拜访开始提醒
     */
    public function mvstatusnotice(){
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $time = time();
        $one_hour_ago = date("Y-m-d H:i:s",($time - 1800));
        $twelve_hour_ago = date("Y-m-d H:i:s",($time - 1800*12));
        //定时器1-1：拿到需要提醒的会议数据
        $where = [];
        $where['m_del'] = 1;
        $where['m_notice'] = 1;
        $where['m_status'] = 1;
        $where['m_start'] = ['<=',$one_hour_ago];
        $meets = Db::table('up_meeting')->where($where)->field('m_id,m_name,m_cp_id')->select();
        //定时器1-2：更改会议状态（和提醒分开处理）
        $this->meetStatus();
        //定时器2-1：拿到需要提醒的拜访数据数据
        $where = [];
        $where['v_notice'] = 1;
        $where['v_ask_status'] = 2;
        $where['v_take_status'] = 2;
        $where['v_status'] = 1;
        $where['v_start'] = ['<=',$one_hour_ago];
        $visits = Db::table('up_visit')->where($where)->field('v_id,v_ask_uid,v_take_uid')->select();
        //定时器2-2：更改拜访状态（和提醒分开处理）
        $where = [];
        $where['v_ask_status'] = 2;
        $where['v_take_status'] = 2;
        $where['v_status'] = 1;
        $where['v_start'] = ['<=',$today];
        $where['v_end'] = ['>=',$today];
        Db::table('up_visit')->where($where)->setField('v_status',2);
        $where = [];
        $where['v_ask_status'] = 2;
        $where['v_take_status'] = 2;
        $where['v_status'] = 2;
        $where['v_end'] = ['<',$today];
        Db::table('up_visit')->where($where)->setField('v_status',3);
        //-------------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------------
        //处理会议和拜访提醒
        if($meets){
            foreach ($meets as $k => $v){
                $m_users = Db::table('up_meeting_user')
                    ->where('mu_m_id',$v['m_id'])
                    ->where('mu_status',2)
                    ->field('mu_user_id,mu_auth')
                    ->select();
                if($m_users){
                    foreach ($m_users as $ku => $vu){
                        $cp_text = '';
                        if($v['m_cp_id'] && $v['m_cp_id'] > 0){
                            $cp_name = Db::table('up_company_product')->where('cp_id',$v['m_cp_id'])->value('cp_name');
                            $cp_text = $cp_name . '产品的';
                        }
                        if($vu['mu_auth'] == 3){
                            $content = '您发布的'.$cp_text.$v['m_name'].'会议还有1小时就要开始了，请知悉：';
                        }else{
                            $content = '您预约的'.$v['m_name'].'会议还有1小时就要开始了，请知悉：';
                        }
                        allInNotice('会议开始提醒',$content,2,$vu['mu_user_id'],$v['m_id']);
                        //----
                        try {
                            $this->Jpush(1, $vu['mu_user_id'], '会议还有1小时就要开始了', ['id' => $v['m_id'], 'n_type' => 2]);
                        } catch (\Exception $e) {
                        }
                        //----
                    }
                }
                Db::table('up_meeting')->where('m_id',$v['m_id'])->setField('m_notice',2);;
            }
        }
        if($visits){
            foreach ($visits as $k => $v){
                $askInfo = Db::table('up_u_user u')
                    ->join('up_company c','u.user_hospital = c.c_id','left')
                    ->join('up_company_team ct','u.user_room = ct.ct_id','left')
                    ->where('user_id',$v['v_ask_uid'])
                    ->field('user_username,c_name,ct_name')
                    ->find();
                $takeInfo = Db::table('up_u_user')
                    ->where('user_id',$v['v_take_uid'])
                    ->field('user_username,user_hospital,user_room')
                    ->find();
                $ask_content = '您与'.$takeInfo['user_hospital'].$takeInfo['user_room'].$takeInfo['user_username'].'预约的拜访还有1个小时就开始了，请知悉：';
                $take_content = $askInfo['c_name'].$askInfo['ct_name'].$askInfo['user_username'].'与您预约的拜访还有1个小时就开始了，请知悉：';
                allInNotice('拜访提醒',$ask_content,3,$v['v_ask_uid'],$v['v_id']);
                allInNotice('拜访提醒',$take_content,3,$v['v_take_uid'],$v['v_id']);
                Db::table('up_visit')->where('v_id',$v['v_id'])->setField('v_notice',2);
                //----
                try {
                    $this->Jpush(1, [$v['v_ask_uid'],$v['v_take_uid']], '拜访还有1个小时就开始了', ['id' => $v['v_id'], 'n_type' => 1]);
                } catch (\Exception $e) {
                }
                //----
            }
        }
        //----------------项目状态----------
        Db::table('up_project')
            ->where('pro_del',1)
            ->where('pro_status',1)
            ->where('pro_start_time','<=',$today)
            ->setField('pro_status',2);
        Db::table('up_project')
            ->where('pro_del',1)
            ->where('pro_status',2)
            ->where('pro_end_time','<=',$today)
            ->setField('pro_status',3);
    }
    /**
     * 一天跑一次（每天的凌晨1点跑，把PC端的二维码清除掉）
     */
    public function pccodedel(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态;定时器3：未答题的一小时后关闭
        //1-1：30天未收货自动收货
        $yestoday = date('Y-m-d',strtotime('-1 day'));
        $where['pc_time'] = ['<=',$yestoday . ' 23:59:59'];
        $res = Db::table('up_z_pcqr')
            ->where($where)
            ->find();
        if($res){
            Db::table('up_z_pcqr')
                ->where($where)
                ->delete();
            //清除图片
            $dir = ROOT_PATH . 'public' . DS . 'uploads/ercode/'.date('Ymd',strtotime('-1 day'));
            $this->deldir($dir);
        }
        exit();
    }
    public function deldir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . '/' . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->deldir($fullpath);
                }
            }
        }
        closedir($dh);
        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 下载
     */
    public function dowload() {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            echo "<script> location.href='https://itunes.apple.com/cn/app/id1516439349'; </script>";
        }else{
           echo "<script> location.href='https://a.app.qq.com/o/simple.jsp?pkgname=com.paixun.fz'; </script>";
        }

    }

    /**
     * PC登录的二维码的地址（不让扫）
     * @return \think\response\Json
     */
    public function qrcode() {
        echo '请使用APP进行扫码！';
        exit;
    }
    /**
     * 测试hook
     */
    public function test() {
        $a= [
            ['name'=>1,11],
            ['name'=>1,22],
            ['name'=>1,33],
        ];

        $b = array_column($a,'name');
        $b = array_unique($b);
        dump($b);die;

        $real1 = [1,2];
        $cp_id = 1;
        $c_id = 1;
        $have = Db::table('up_user_charge')->alias('uc')
            ->join('up_u_user u','uc.uc_user_id=u.user_id','LEFT')
            ->where('uc_shi','in',$real1)
            ->where('uc_c_id',$c_id)
            ->group('uc_user_id')
            ->having('count(*) >= 3')
            ->column('uc_user_id');
        dump($have);
        die;
        $have = Db::table('up_user_charge')->alias('uc')
            ->join('up_u_user u','uc.uc_user_id=u.user_id','LEFT')
            ->where('uc_shi','in',$real1)
            ->where('user_status',1)
            ->where('user_auth',1)
            ->where('uc_cp_ids','like',"%,$cp_id,%")
            ->field('user_id,user_username,user_image')
            ->column('uc_user_id');
    }


}